package com.anlogic.sdk.toolpreference.ui;

import org.apache.logging.log4j.Level;
import org.eclipse.jface.preference.ComboFieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

import com.anlogic.sdk.toolpreference.Activator;
import com.anlogic.sdk.toolpreference.PreferenceConstants;
import com.anlogic.sdk.utils.LogUtils;

public class LogPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {
	private ComboFieldEditor logLevelFieldEditor;

	private static String[][] logLevels = new String[][] { { "Trace", Level.TRACE.name() },
			{ "Debug", Level.DEBUG.name() }, { "Info", Level.INFO.name() }, { "Warning", Level.WARN.name() },
			{ "Error", Level.ERROR.name() } };

	public LogPreferencePage() {
		super(FieldEditorPreferencePage.GRID);
		setPreferenceStore(Activator.getDefault().getPreferenceStore());
	}

	@Override
	protected void createFieldEditors() {
		logLevelFieldEditor = new ComboFieldEditor(PreferenceConstants.LOG_LEVEL, "Log Level: ", logLevels,
				getFieldEditorParent());
		addField(logLevelFieldEditor);
	}

	@Override
	protected void performApply() {
		super.performApply();
		updateConfigurations();
	}

	@Override
	public boolean performOk() {
		boolean flag = super.performOk();
		updateConfigurations();
		return flag;
	}

	private void updateConfigurations() {
		IPreferenceStore preferences = Activator.getDefault().getPreferenceStore();
		String level = preferences.getString(PreferenceConstants.LOG_LEVEL);
		LogUtils.setLevel(level);
	}

	@Override
	public void init(IWorkbench workbench) {
		// TODO Auto-generated method stub

	}
}
